<template>
  <div id="newestsong">
    <el-row type="flex" justify="center">
      <el-col :xs="14" :sm="14" :md="14" :lg="14" :xl="14">
        <div class="tab-song">
          <router-link  to="/homepage/contents/newestsong" tag="div"
            class="song-express"
            :class="{ active: tabSong === 0 }"
            @click="tabSong = 0"
          >
            新歌速递
          </router-link>
          <router-link to="/homepage/contents/newdisc" tag="div"
            class="song-putaway"
            :class="{ active: tabSong === 1 }"
            @click="tabSong = 1"
          >
            新碟上架
          </router-link>
        </div>
        <div class="hd-title">
          <ul>
            <li
              v-for="(item, index) in songExpress"
              :key="index"
              :class="{ active: tag === index }"
              @click="tabTag(item.type);tag = index"
            >
              {{ item.name }}
            </li>
          </ul>
          <div class="btns">
            <div class="play-all btn-all" @click="addList">
              <i class="el-icon-video-play" ></i>播放全部
            </div>
            <div class="collect-all btn-all">
              <i class="el-icon-folder-add"></i>收藏全部
            </div>
          </div>
        </div>
        <ul class="list">
          <li
            v-for="(item, index) in songList"
            :key="index"
            class="flex_space_btween"
            :class="{ active: index % 2 === 0 }"
             @click="play(item)"
          >
            <div class="music-title flex_start">
              <span>{{index = index+1 &lt; 10 ? '0' +(index+1):index+1}}</span>
              <div class="images-mu">
                  <div class="icon-mu">
                      <img src="../ranklist/images/play.png" alt="">
                  </div>
                  <img v-lazy="item.album.picUrl" alt="">
              </div>
              <span class="ms-name nowrap">{{ item.name }}</span>

              <div class="mv flex_center sq">SQ</div>
              <div class="mv flex_center" :class="{ active: item.mvid }">MV</div>
            </div>
            <p class="nowrap">
              <span v-for="(item, index) in item.artists" :key="index">
                 <span v-if="index>0">/</span> {{item.name}}
                  </span>
            </p>
            <p class="nowrap">{{ item.album.name }}</p>
            <p>
              {{ Math.floor(item.duration / 1000 / 60) }}:{{
            Math.floor((item.duration / 1000) % 60) &lt; 10 ? '0' +(Math.floor((item.duration / 1000) % 60)):Math.floor((item.duration / 1000) % 60)
              }}
            </p>
          </li>
        </ul>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "newestsong",
  data() {
    return {
      tabSong: 0,
      tag: 0,
      songExpress: [
        {
          type: 0,
          name: "全部",
        },
        {
          type: 7,
          name: "华语",
        },
        {
          type: 96,
          name: "欧美",
        },
        {
          type: 8,
          name: "日本",
        },
        {
          type: 16,
          name: "韩国",
        },
      ],
      songList: [],
    };
  },
  created() {
    this.axios({
      withCredentials: true,
      method: "get",
      url: "https://music.hzbiz.net/top/song?timetamp=" + new Date().getTime(),
      params: {
        type: 0,
      },
    }).then((res) => {
      this.songList = res.data.data;
    });
  },
  methods: {
      //标签切换
      tabTag(index){
          this.axios({
      withCredentials: true,
      method: "get",
      url: "https://music.hzbiz.net/top/song?timetamp=" + new Date().getTime(),
      params: {
        type: index,
      },
    }).then((res) => {
      this.songList = res.data.data;
    });
      },
      //播放全部
      addList(){
       this.$store.state.playMusicList.push(...this.songList)
       let obj = {};
          this.$store.state.playMusicList = this.$store.state.playMusicList.reduce(
            function (item, next) {
              obj[next.id] ? "" : (obj[next.id] = true && item.push(next));

              return item;
            },
            []
          );
        console.log("添加歌单成功",this.$store.state.playMusicList);
    },
      //播放相关
    play(item) {
      document
        .getElementsByClassName("audio-play")[0]
        .classList.add("audio-stop");

      this.axios({
        withCredentials: true,
        method: "get",
        url:
          "https://music.hzbiz.net/song/url?timetamp=" + new Date().getTime(),
        params: {
          id: item.id,
        },
      }).then((res) => {
        this.axios({
          withCredentials: true,
          method: "get",
          url:
            "https://music.hzbiz.net/song/detail?timetamp=" +
            new Date().getTime(),
          params: {
            ids: item.id,
          },
        }).then((res) => {
          console.log("歌曲详情", res.data);
          this.currentMusic = res.data.songs[0];
          this.$store.state.nowMusic = this.currentMusic;
          this.$store.state.playMusicList.push(this.currentMusic);
          //数组对象去重
          let obj = {};
          this.$store.state.playMusicList = this.$store.state.playMusicList.reduce(
            function (item, next) {
              obj[next.id] ? "" : (obj[next.id] = true && item.push(next));

              return item;
            },
            []
          );
          this.axios({
            withCredentials: true,
            method: "get",
            url:
              "https://music.hzbiz.net/lyric?timetamp=" + new Date().getTime(),
            params: {
              id: this.$store.state.nowMusic.id,
            },
          }).then((res) => {
            this.$store.commit("saveLyric", res.data.lrc.lyric);
            console.log("歌词存储成功");
          });
          //评论获取
          this.axios({
            withCredentials: true,
            method: "get",
            url:
              "https://music.hzbiz.net/comment/music?timetamp=" +
              new Date().getTime(),
            params: {
              id: this.$store.state.nowMusic.id,
            },
          }).then((res) => {
            this.$store.commit("saveComment", res.data);
            console.log("歌曲评论储存成功", this.$store.state.songcomment);
          });
          console.log("添加歌单成功", this.$store.state.playMusicList);
          console.log("仓库现在的音乐", this.$store.state.nowMusic);
        });
        // console.log(res.data);
        let aud = document.getElementsByTagName("audio")[0];
        aud.setAttribute("src", res.data.data[0].url);
        aud.play();
      });
    },
  },
};
</script>

<style  scoped>
.tab-song {
  margin: 0 auto;
  width: 234px;
  height: 32px;
  border-radius: 16px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.tab-song > div {
  width: 50%;
  border-radius: 16px;
  cursor: pointer;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.tab-song > div:hover {
  background-color: #f1efef;
}
.tab-song > div.active {
  color: #ffffff;
  background-color: #bbbbbb;
}
.hd-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  margin-bottom: 20px;
}
.hd-title .btns {
  display: flex;
}
.hd-title .btns .btn-all {
  cursor: pointer;
  width: 95px;
  height: 26px;
  border-radius: 13px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid rgba(0, 0, 0, 0.1);
}
.hd-title .btns .btn-all > i {
  margin-right: 3px;
}
.hd-title .btns .play-all {
  background-color: #ec4141;
  color: #ffffff;
  margin-right: 10px;
}
.hd-title ul {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.hd-title ul li {
  margin-left: 27px;
  cursor: pointer;
}
.hd-title ul li:hover {
  color: rgb(19, 18, 18);
}
.hd-title ul li.active {
  color: #000;
  font-weight: bold;
}

.list li {
    padding: 10px 0;
  box-sizing: border-box;
  padding-left: 28px;
  color: rgba(0, 0, 0, 0.4);
}
.list li.active {
  background-color: #f9f9f9;
}
.list li:hover {
  background-color: #f2f2f2;
}
.list p {
  box-sizing: border-box;
}
.list .music-title {
  width: 800px;
}
.list .music-title .ms-name {
  color: #000;
  max-width: 200px;
}

.list p:nth-child(2) {
  width: 300px;
  margin-right: 10px;
}
.list p:nth-child(3) {
  width: 370px;
  margin-right: 10px;
}
.list p:nth-child(4) {
  width: 150px;
}

.list li .music-title .images-mu{
    width: 60px;
    height: 60px;
    overflow: hidden;
    border-radius: 5px;
    position: relative;
    margin-right: 8px;
    cursor: pointer;
}
.list li .music-title .images-mu .icon-mu{
    position: absolute;
    width: 26px;
    height: 26px;
    overflow: hidden;
    border-radius: 50%;
    background-color: #ffffff;
    top: calc(50% - 13px);
    left: calc(50% - 13px);
    display: flex;
    justify-content: center;
    align-items: center;
}
.list li .music-title .images-mu .icon-mu img{
    width: 60%;
    margin-left: 4px;
}
.list li .music-title span:first-child {
  margin-right: 30px;
  display: block;
  width: 25px;
}

.list li .music-title .after {
  margin-right: 17px;
}
.list li .music-title .mv {
  border: 1px solid #e41;
  border-radius: 3px;
  color: #e41;
  width: 27px;
  height: 12px;
  font-size: 12px;
  margin-left: 10px;
  display: none;
}
.list li .music-title .sq {
  display: flex;
}
.list li .music-title .mv.active {
  display: flex;
  cursor: pointer;
}
.list li .music-title .mv.vip {
  display: flex;
  cursor: pointer;
}
</style>